/*
 * @(#)BbsForgotController.java 2019年9月30日 下午5:39:15
 * Copyright 2019 zmr, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.dev.web.controller.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsUserService;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.utils.RedisUtils;
import com.dev.common.utils.StringUtils;
import com.dev.framework.shiro.service.BbsPasswordService;
import com.dev.framework.util.ShiroUtils;

/**
 * <p>File：BbsForgotController.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2019 2019年9月30日 下午5:39:15</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@Controller
@RequestMapping("forgot")
public class BbsForgotController extends BaseController
{
    private String             prefix = "common";

    @Autowired
    private IBbsUserService    bbsUserService;

    @Autowired
    private BbsPasswordService passwordService;

    /**
     * 找回密码
     */
    @GetMapping()
    public String forgot()
    {
        return prefix + "/forgot";
    }

    /**
     * 忘记密码--发送邮件
     */
    @PostMapping("send")
    @ResponseBody
    public AjaxResult forgotSend(@RequestParam(value = "username", required = true) String username)
    {
        bbsUserService.forgotPwdSendMail(username);
        return success();
    }

    /**
     * 找回密码---重置
     */
    @GetMapping("rset/{code}")
    public String forgotRestPage(@PathVariable("code") String code, ModelMap mmap)
    {
        Long userId = (Long) RedisUtils.get(code);
        String username = null;
        if (userId != null)
        {
            BbsUser bbsUser = bbsUserService.selectBbsUserById(userId);
            username = bbsUser.getUsername();
            mmap.addAttribute("code", code);
            mmap.addAttribute("username", username);
        }
        return prefix + "/forgotMail";
    }

    /**
     * 找回密码---重置
     */
    @PostMapping("rset")
    @ResponseBody
    public AjaxResult forgotRest(String code, String password)
    {
        if (StringUtils.isEmpty(code))
        {
            return error("请打开正确链接");
        }
        if (StringUtils.isEmpty(password))
        {
            return error("请打开正确链接");
        }
        Long userId = (Long) RedisUtils.get(code);
        if (null == userId)
        {
            return error("该链接已失效");
        }
        BbsUser bbsUser = bbsUserService.selectBbsUserById(userId);
        if (bbsUser == null)
        {
            return error("未知异常，请联系管理员");
        }
        bbsUser.setSalt(ShiroUtils.randomSalt());
        bbsUser.setPassword(passwordService.encryptPassword(bbsUser.getUsername(), password, bbsUser.getSalt()));
        bbsUserService.resetUserPwd(bbsUser);
        // 删除Key
        RedisUtils.del(code);
        return AjaxResult.success();
    }
}
